<template>
	<view class="container">
		<view class="uni-form-item">
			<text class="reg_label">注册</text>
		</view>
		<view class="username">
			<text>用户名</text><input class="uni-input" v-model="username" />
		</view>
		<view class="password">
			<text>密码</text>
			<view class="pwd">
				<input class="uni-input" v-model="password" :password="!showPassword" style="width: 100%;" />
				<!-- 显示或隐藏密码 -->
				<image @click="()=>{showPassword=!showPassword;}"
					:src="showPassword?'/static/pwd/eye-open.png':'/static/pwd/eye-close.png'"
					style="position: absolute; z-index: 2;  height: 60rpx; width: 60rpx; margin-top: 20rpx; right: 20rpx;">
				</image>
			</view>
		</view>
		<view class="password">
			<text>确认密码</text>
			<view class="pwd">
				<input class="uni-input" v-model="cPassword" :password="!showCPassword" style="width: 100%;" />
				<!-- 显示或隐藏密码 -->
				<image @click="()=>{showCPassword=!showCPassword;}"
					:src="showCPassword?'/static/pwd/eye-open.png':'/static/pwd/eye-close.png'"
					style="position: absolute; z-index: 2;  height: 60rpx; width: 60rpx; margin-top: 20rpx; right: 20rpx;">
				</image>
			</view>
		</view>
		<!-- 验证码 -->
		<view class="capt">
			<view class="capt-left">
				<text class="capt-text">验证码</text><input class="uni-input" v-model="capt" />
			</view>
			<image class="img" :src="imgSrc" @click="getCaptcha"></image>
		</view>
		<view class="password">
			<text>手机号</text><input class="uni-input" v-model="phone" />
		</view>
		<view class="sms-capt">
			<view class="capt-left">
				<text class="capt-text">手机验证码</text><input class="uni-input" v-model="smsCapt" />
			</view>
			<button v-if="!isSend" type="primary" class="btn-send" @click="sendSms" :disabled="isSend">发送验证码</button>
			<button type="primary" class="btn-sending" v-else :disabled="true">
				还剩 {{resTime}} 秒
			</button>
		</view>
		<view class="btn">
			<view>
				<button type="primary" class="btn-reg" @click="regUser">注册</button>
			</view>
			<view style="margin-top: 50rpx;">
				<text>@Copyright By QAQ</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showPassword: false,
				showCPassword: false,
				imgSrc: "",
				username: "",
				password: "",
				cPassword: "",
				phone: "",
				capt: "",
				captId: 0,
				captKey: "",
				smsCapt: "",

				cTimer: null,
				isSend: false,
				timer: null,
				resTime: 60,
			};
		},
		onLoad() {
			this.getCaptcha()
		},
		methods: {
			//注册用户
			async regUser() {
				if (this.check() == null) return;
				const res = await uni.$dapi.postReg({
					username: this.username,
					password: this.password,
					phone: this.phone,
					smsCode: this.smsCapt,
				})
				if (res === true) {
					uni.$showMsg("注册成功")
					setTimeout(() => {
						uni.navigateBack({
							delta: 1, //返回层数，2则上上页
						})
					}, 3)
				}
				// console.log(res);
			},

			// 获取验证码
			async getCaptcha() {
				if (this.cTimer) return uni.$showMsg("5秒后再点击")
				this.cTimer = setTimeout(() => {
					this.cTimer = null
				}, 5000)
				const res = await uni.$dapi.getCaptcha();
				console.log(res);
				if (res == null) return
				this.imgSrc = res.image
				this.captId = res.codeId
				this.captKey = res.key
			},

			//发送短信验证码
			async sendSms() {
				const res = await uni.$dapi.getSmsCode(this.phone)
				console.log(res);

				this.isSend = true;
				this.timer = setInterval(() => {
					// console.log("hello ", this.isSend, this.resTime);
					if (this.tick()) {
						this.isSend = false
						this.resTime = 60
						clearInterval(this.timer)
					}
				}, 1000)
			},
			tick() {
				this.resTime--;
				if (this.resTime > 0) return false;
				return true;
			},
			check() {
				if (this.username.length < 3 || this.username.length > 20) {
					return uni.$showMsg("用户名需3-20个字符");
				}
				if (this.password.length < 3 || this.password.length > 20) {
					return uni.$showMsg("密码需3-20个字符");
				}
				if (this.cPassword.length < 3 || this.cPassword.length > 20) {
					return uni.$showMsg("确认密码需3-20个字符");
				}
				if (this.password !== this.cPassword) {
					return uni.$showMsg("确认密码不一致");
				}
				if (this.capt.length !== 5) {
					return uni.$showMsg("验证码格式不正确");
				}
				const reg = /^1[3456789]\d{9}$/;

				if (this.phone.length < 5 || !reg.test(this.phone)) {
					return uni.$showMsg("手机号格式不正确");
				}
				if (this.smsCapt.length !== 6) {
					return uni.$showMsg("手机验证码格式不正确");
				}

				if (this.capt.toLocaleLowerCase() !== this.captKey) {
					return uni.$showMsg("验证码错误")
				}
				return true;
			},
		}
	}
</script>

<style lang="scss">
	.container {
		height: fit-content;
		padding: 20rpx 40rpx;
		// margin: 200rpx 20rpx;
		text-align: center;
		background-color: #c6d1d5;

		.username {
			margin-top: 10rpx;
		}

		.password {
			margin-top: 10rpx;

			.pwd {
				margin-top: 10rpx;
				display: flex;
				flex-direction: row;
				position: relative;
			}
		}

		.capt {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			margin-top: 10rpx;

			.capt-left {
				.capt-text {
					white-space: nowrap;
				}
			}

			.img {
				height: 100rpx;
				width: 300rpx;
				margin-top: 30rpx;
			}

		}

		.sms-capt {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			margin-top: 10rpx;

			.capt-left {
				.capt-text {
					white-space: nowrap;
				}
			}

			.btn-send {
				margin-top: 50rpx;
				width: 240rpx;
				height: 80rpx;
			}

			.btn-sending {
				color: #9faca7;
				margin-top: 50rpx;
				width: 240rpx;
				height: 80rpx;
			}

		}



		.reg_label {
			color: #0889ac;
			margin: 0 auto;
			font-size: 60rpx;
		}

		.uni-input {
			margin-top: 10rpx;
			background-color: #fff;
			height: 80rpx;
			text-align: left;
			padding-left: 10rpx;
			border-radius: 10rpx;
			font-size: 30rpx;
		}

		.uni-form-item {
			margin: 30rpx 0;
		}

		button {
			font-size: 30rpx;
		}

		.btn {
			// display: flex;
			// flex-direction: column;
			// justify-content: center;
			// align-items: center;
			margin-top: 80rpx;
			text-align: center;

			.btn-reg {
				width: 90%;
				border-radius: 100px;
				margin: 0px auto;
				background-color: #12e594c3;
			}
		}

	}
</style>